﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OBUActivation.Tags
{
    class CommonFunc
    {

        /// <summary>
        /// 获取报文头的内容
        /// </summary>
        /// <param name="FullMessage"></param>
        /// <returns></returns>
        private static byte[] GetMessageHead(byte[] FullMessage)
        {
            if (FullMessage.Length < BaseMsg.HEAD_LENGTH)
            {
                return new byte[] { };
            }
            byte[] bodyMsg = new byte[BaseMsg.HEAD_LENGTH];
            Buffer.BlockCopy(FullMessage, 0, bodyMsg, 0, BaseMsg.HEAD_LENGTH);
            return bodyMsg;
        }

        /// <summary>
        /// 获取报文体报文内容
        /// </summary>
        /// <param name="FullMessage">完整的报文信息</param>
        /// <returns></returns>
        private static byte[] GetMessageBody(byte[] FullMessage)
        {
            
            if (FullMessage.Length < BaseMsg.HEAD_LENGTH)
            {
                return new byte[] { };
            }
            byte[] bodyMsg = new byte[FullMessage.Length - BaseMsg.HEAD_LENGTH-BaseMsg.END_LENGTH];
            Buffer.BlockCopy(FullMessage, BaseMsg.HEAD_LENGTH, bodyMsg, 0, bodyMsg.Length);
            return bodyMsg;
        }

        /// <summary>
        /// 打包发送之前的数据，
        /// 首先经过加密，再加上异或校验码，然后转义，最后加上报文头和报文尾
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static byte[] PackageMsg(byte[] p)
        {
            //p = EncryptAndDecrypt.EncryptMessage(p);
            p = TransAndRetrans.AddCRC(p, p.Length);
            //p = TransAndRetrans.AddFCS(p, p.Length);
            p = TransAndRetrans.TransCode(p, p.Length);
            p = CommonFunc.FillHeadEnd(p);
            return p;
        }

        /// <summary>
        /// 解包接收到的数据
        /// 首先去掉报文头，再反转义得到报文去掉校验码FCS 和异或校验码，最后解密数据
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static byte[] UnpackageMsg(byte[] p)
        {
            if (p == null || p.Length == 0)
            {
                return null;
            }
            p = TransAndRetrans.ReTransCode(p, p.Length);
            // 校验FCS
            /*if (!TransAndRetrans.CheckFCS(p, p.Length))
                return null;
            byte[] s = new byte[p.Length - 2];
            Buffer.BlockCopy(p, 0, s, 0, s.Length);
            */
            // 校验CRC
            byte[] s = new byte[p.Length - 1];
            if (!TransAndRetrans.IsCRCCorrect(s))
                return null;
            
            Buffer.BlockCopy(p, 0, s, 0, s.Length);
            // s = EncryptAndDecrypt.DecryptMessage(s);
            return s;
        }

        //添加BLE头尾
        public static byte[] FillHeadEnd(byte[] msg)
        {
            List<byte> list = new List<byte>();
            byte[] head = new byte[] { 0xFF, 0xFF };
            byte end = 0xFF;
            list.AddRange(head);
            list.AddRange(msg);
            list.Add(end);

            return list.ToArray();
        }

       
        public static byte[] RemoveHeadEnd(byte[] msg)
        {
            if (msg[0] == 0xFF && msg[1] == 0xFF && msg[msg.Length - 1] == 0xFF)
            {
                byte[] result = new byte[msg.Length - 3];
                Buffer.BlockCopy(msg, 2, result, 0, msg.Length - 3);
                return result;
            }
            return null;
        }
    }
}
